package wpRelations;

import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PVector;

public class wNode {
	PVector pos;
	PApplet parent;
	String title;
	int type; //"personZero", "person", "nonPerson", "category"
	int subType; //"sub", "super"
	public static final int PERSON_ZERO = 0;
	public static final int PERSON = 1;
	public static final int NON_PERSON = 2;
	public static final int CATEGORY = 3;
	ArrayList<wNode> nodes;
	PFont myFont;
	int[] myColors;
	public int index;
	int connections, maxConnections, minConnections;
	float minPosRadius, maxPosRadius;
	float minDisplayRadius, maxDisplayRadius, myDisplayRadius;
	float mass;
	
	public wNode(PApplet p5, String title, ArrayList<wNode> theNodes, int theType) {
		parent = p5;
		this.title = title;
		nodes = theNodes;
		type = theType;
		minConnections = 0;
		maxConnections = 100;
		minDisplayRadius = 30f;
		maxDisplayRadius = 100f;
		minPosRadius = 30f;
		maxPosRadius = p5.height/2;
		//testing:
		connections = (int)parent.random(100);
		initColors();
	}
	
	public wNode(PApplet p5,String title, ArrayList<wNode> theNodes, int theType, int theIndex) {
		parent = p5;
		this.title = title;
		nodes = theNodes;
		type = theType;
		minConnections = 0;
		maxConnections = 100;
		minDisplayRadius = 30f;
		maxDisplayRadius = 100f;
		minPosRadius = 30f;
		maxPosRadius = p5.height/2;
		index = theIndex;
		//testing:
		connections = (int)parent.random(100);
		initColors();
	}
	
	public void setMaxConnections(int theMaxConnections) {
		maxConnections = theMaxConnections;
	}
	
	public void setMinConnections(int theMinConnections) {
		minConnections = theMinConnections;
	}
	
	public void addConnection() {
		connections++;
	}
	
	public void setType(int theType) {
		type = theType;
	}
	
	public int getType() {
		return type;
	}
	
	public int getSubType() {
		return subType;
	}
	
	public void setSubType (int theSubType) {
		subType = theSubType;
	}
	
	private void initColors() {
		 myColors = new int[4];
		 myColors[0] = parent.color(255,0,0,127);
		 myColors[1] = parent.color(127,0,127,127);
		 myColors[2] = parent.color(0,127,255,127);
		 myColors[2] = parent.color(0,127,127,127);
	}
	
	
	
	public void initPos () {
		switch (type) {
		case PERSON_ZERO:
			pos = new PVector(0,0,0);
			break;
		case PERSON:
			float lambda = parent.random(parent.TWO_PI);
			float beta = parent.random(parent.PI)-parent.HALF_PI;
			float r = (maxPosRadius-minPosRadius)*connections/maxConnections+minPosRadius;
			pos = new PVector(parent.cos(beta)*parent.sin(lambda)*r,parent.sin(beta)*r,parent.cos(beta)*parent.cos(lambda)*r);
			break;
			
		case NON_PERSON:
			lambda = parent.random(parent.TWO_PI);
			beta = parent.random(parent.PI)-parent.HALF_PI;
			r = (maxPosRadius-minPosRadius)*connections/maxConnections+minPosRadius;
			pos = new PVector(parent.cos(beta)*parent.sin(lambda)*r,parent.sin(beta)*r,parent.cos(beta)*parent.cos(lambda)*r);
			break;

		default:
			pos = new PVector(parent.random(parent.width),parent.random(parent.height),0);
			break;
		}
	}
	
	public void update() {
		
	}
	
	public void display() {
		parent.pushStyle();
		parent.ellipseMode(parent.CENTER);
		parent.pushMatrix();
		parent.translate(pos.x, pos.y,pos.z);
		parent.noStroke();
		switch (type) {
		case PERSON_ZERO:
			parent.fill(parent.color(myColors[0]));
			break;
		case PERSON:
			parent.fill(parent.color(myColors[1]));
			break;
		case NON_PERSON:
			parent.fill(parent.color(myColors[2]));
			break;
		case CATEGORY:
			parent.fill(parent.color(myColors[3]));
			break;

		default:
			parent.fill(parent.color(0,127));
			break;
		}
		myDisplayRadius = (maxDisplayRadius-minDisplayRadius)*connections/maxConnections+minDisplayRadius;
		//myDisplayRadius = (maxDisplayRadius-minDisplayRadius)*pos.mag()/maxConnections+minDisplayRadius;
		parent.ellipse(0,0,myDisplayRadius,myDisplayRadius);
	      parent.fill(0);
	      parent.textAlign(parent.CENTER, parent.CENTER);
	      parent.text(title, 0, 0, 0);

		parent.popMatrix();
		parent.popStyle();
		
	}
	
}
